﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Warehouse.Domain.DTO;
using Warehouse.Read.API.Applcation.Command.OutBoundCommand;
using Warehouse.Read.API.Dtos;

namespace Warehouse.Read.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class OutBoundController : ControllerBase
    {
        private readonly IMediator _mediator;


        public OutBoundController(IMediator mediator)
        {
            _mediator = mediator;

        }


        /// <summary>
        /// 获取出库单列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<OutShipShowDTO>>> GetOutSHipList([FromQuery] SelectOutBountCommand command)
        {
            ApiResult<PageDTO<OutShipShowDTO>> result = new ApiResult<PageDTO<OutShipShowDTO>>();

            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Uid = Convert.ToInt32(uid);
                command.Oid = sid;
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }



        /// <summary>
        /// 反填出库单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<OutShipShowDTO>>> GetOutShipDetail([FromQuery] FillOutShipCommand command)
        {
            ApiResult<List<OutShipShowDTO>> result = new ApiResult<List<OutShipShowDTO>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;

                throw;
            }
            return result;
        }
    }
}
